package com.herui.iot.ui.view.ecg;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;

import com.herui.sdyu_lib.utils.LogUtils;
import com.herui.sport_care.config.ECGConfig;
import com.herui.sport_care.ui.view.ecg.bean.EcgShowType;
import com.herui.sport_care.ui.view.ecg.buff.MultiChannelBufferBT;


/**
 * Created by amanda.shan on 2017/1/13.
 */

public class RealEcgView extends AnimEcgView implements RealEcgShowInterface {

    private static final String TAG = "RealEcgView";

    // 心电数据
    private MultiChannelBufferBT dbi;
    private RealEcgShowInterface realEcgShowInterface;

    private int start = -1;

    private boolean singleEcg = true;

    @Override
    public void fling(float flingX, float flingY) {

    }

    public RealEcgView(Context context) {
        super(context);
    }

    public RealEcgView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RealEcgView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setEcgShowType(EcgShowType ecgShowType) {
        super.setEcgShowType(ecgShowType);
    }

    public void setSingleEcg(boolean singleEcg) {
        this.singleEcg = singleEcg;
    }

    protected long recordTime;

    @Override
    public void run() {

        while (isRunning) {

            if (!surfaceHolder.getSurface().isValid()) {
                continue;
            }
            Canvas canvas = surfaceHolder.lockCanvas();
            synchronized (syn) {

                if (recordTime == 0)
                    recordTime = System.currentTimeMillis();
                else {
                    if (System.currentTimeMillis() - recordTime > 100) {
                        long time_diff = System.currentTimeMillis() - recordTime;
                        recordTime = System.currentTimeMillis();
                        update(time_diff);
                    }
                }

                if (canvas != null) {
                    if (singalWaveView == null) {
                        boolean isAnim = false;
                        for (LeadView leadView : leadViews) {
                            if (leadView.isAniming()) {
                                isAnim = true;
                                break;
                            }
                        }

                        for (LeadView leadView : leadViews)
                            leadView.doDraw(canvas);

                        if (showWaveView != null) {
                            showWaveView.doDraw(canvas);
                            if (!isAnim && !oneLeadType)
                                showWaveView = null;
                        }
                    } else {
                        singalWaveView.doDraw(canvas);
                    }
                }
            }
            if (canvas != null)
                surfaceHolder.unlockCanvasAndPost(canvas);

            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private synchronized void update(long time_diff) {

        if (dbi == null)
            return;

        if (start == -1 || dbi.isFrequencyChange()) {
            start = dbi.getCurrentPT();
            for (LeadView leadView : leadViews) {
                leadView.earsePoints();
            }
            return;
        }

        int width = dbi.getFrequency() / ECGConfig.SHOW_FRE;
        int dis_freq = ECGConfig.SHOW_FRE;
        if (width <= 0) {
            width = 1;
            dis_freq = dbi.getFrequency();
        }
        int cnt = (int) (time_diff * dis_freq / 1000);
        int updatePT = (cnt * width)
                % (dbi.getSecondBuffer() * dbi.getFrequency());
        int updatePTBuffer = getUpdatePTBuffer();

        if (updatePT <= updatePTBuffer) {
            for (int i = 0; i < cnt; i++) {
                for (int leadNo = 0; leadNo < ECGConfig.ECGLEADNAMES.size(); leadNo++) {
                    int currentPT = dbi.getCurrentPT();
                    int value = dbi.getLocalMeanChannel(start, width, leadNo);

                    float v = 0;
                    v = ECGConfig.ECGSingleConvertor(value);

                    for (LeadView leadView : leadViews) {
                        if (leadView.getLeadNo() == leadNo) {
                            leadView.updatePoint(v);
                        }
                    }
                    if (singalWaveView != null && singalWaveView.getLeadNo() == leadNo) {
                        singalWaveView.updatePoint(v);
                    }
                }
                start += width;
                start = start % (dbi.getSecondBuffer() * dbi.getFrequency());
            }

            // 显示和接收的数据相距超过10s
            if (getUpdatePTBuffer() > 10 * dbi.getFrequency()) {

                LogUtils.w("显示和接收的数据相距超过10s: ");
                LogUtils.w(getUpdatePTBuffer() + ">" + 10 * dbi.getFrequency());
                start = dbi.getCurrentPT();
                for (LeadView leadView : leadViews) {
                    leadView.earsePoints();
                }
            }
        } else {
//            LogUtils.d("updatePT > updatePTBuffer:" + updatePTBuffer);
        }
        return;
    }

    private int getUpdatePTBuffer() {
        int buffSize = dbi.getFrequency() * dbi.getSecondBuffer();
        int updatePTBuffer = (dbi.getCurrentPT() + buffSize - start)
                % buffSize;
        return updatePTBuffer;
    }


    @Override
    public void setRealEcgShowInterface(RealEcgShowInterface realEcgShowInterface) {
        this.realEcgShowInterface = realEcgShowInterface;
    }


    @Override
    public void stopRefreshScreem() {
        isRunning = false;
    }

    @Override
    public void setMultiChannelBufferBT(MultiChannelBufferBT dbi) {
        this.dbi = dbi;
    }

    @Override
    public void singleEcg() {
        ECGConfig.SHOW_FRE = 500;
        EcgShowType type = ecgShowType;
        ecgShowType = null;
        setSingleMode(true, 0);
        singleEcg = true;
    }

    @Override
    public void mutilEcg() {
        ECGConfig.SHOW_FRE = 75;
        EcgShowType type = ecgShowType;
        ecgShowType = null;
        setEcgShowType(type);
        singleEcg = false;
    }

    public interface HeartLis {
        public void heartChage(int heart);
    }

}
